View Javadoc

1   /* ====================================================================
2    * The Apache Software License, Version 1.1
3    *
4    * Copyright (c) 2003 The Apache Software Foundation.  All rights
5    * reserved.
6    *
7    * Redistribution and use in source and binary forms, with or without
8    * modification, are permitted provided that the following conditions
9    * are met:
10   *
11   * 1. Redistributions of source code must retain the above copyright
12   *    notice, this list of conditions and the following disclaimer.
13   *
14   * 2. Redistributions in binary form must reproduce the above copyright
15   *    notice, this list of conditions and the following disclaimer in
16   *    the documentation and/or other materials provided with the
17   *    distribution.
18   *
19   * 3. The end-user documentation included with the redistribution,
20   *    if any, must include the following acknowledgment:
21   *       "This product includes software developed by the
22   *        Apache Software Foundation (http://www.apache.org )."
23   *    Alternately, this acknowledgment may appear in the software itself,
24   *    if and wherever such third-party acknowledgments normally appear.
25   *
26   * 4. The names "Apache" and "Apache Software Foundation" and
27   *    "Apache Maven" must not be used to endorse or promote products
28   *    derived from this software without prior written permission. For
29   *    written permission, please contact apache@apache.org.
30   *
31   * 5. Products derived from this software may not be called "Apache",
32   *    "Apache Maven", nor may "Apache" appear in their name, without
33   *    prior written permission of the Apache Software Foundation.
34   *
35   * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
36   * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
37   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
38   * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
39   * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
40   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
41   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
42   * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
43   * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
44   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
45   * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
46   * SUCH DAMAGE.
47   * ====================================================================
48   *
49   * This software consists of voluntary contributions made by many
50   * individuals on behalf of the Apache Software Foundation.  For more
51   * information on the Apache Software Foundation, please see
52   * <http://www.apache.org >.
53   *
54   * ====================================================================
55   */
56  
57  package org.apache.maven.sitevalidator;
58  
59  import java.io.File;
60  import java.io.IOException;
61  
62  import org.apache.tools.ant.Project;
63  import org.dom4j.Element;
64  import org.dom4j.io.XMLWriter;
65  import org.dom4j.tree.DefaultElement;
66  import org.xml.sax.EntityResolver;
67  import org.xml.sax.ErrorHandler;
68  import org.xml.sax.InputSource;
69  import org.xml.sax.SAXParseException;
70  
71  /***
72   * ValidatorHandler
73   */
74  class ValidatorHandler implements ErrorHandler, EntityResolver
75  {
76  
77      /***
78       * file being validated
79       */
80      private File currentFile = null;
81  
82      /***
83       * number of errors found in the current file
84       */
85      private int errorsFound = 0;
86  
87      /***
88       * out
89       */
90      private XMLWriter out;
91  
92      /***
93       * set XMLWriter for output
94       * @param writer XMLWriter
95       */
96      public void setOut(XMLWriter writer)
97      {
98          this.out = writer;
99      }
100 
101     /***
102      * init the validation handler: set up current file and reset errors number
103      * @param file File currently validated
104      */
105     public void init(File file)
106     {
107         currentFile = file;
108         errorsFound = 0;
109     }
110 
111     /***
112      * how many errors happened during last parsing?
113      * @return int number of errors
114      */
115     public int getErrors()
116     {
117         return errorsFound;
118     }
119 
120     /***
121      * an error occurred: increment errors count and log exception message
122      * @param exception SAXParseException
123      */
124     public void error(SAXParseException exception)
125     {
126         errorsFound++;
127         doLog(exception, Project.MSG_ERR);
128     }
129 
130     /***
131      * a fatal error occurred: redirect to error()
132      * @param exception SAXParseException
133      */
134     public final void fatalError(SAXParseException exception)
135     {
136         error(exception);
137     }
138 
139     /***
140      * a warning occurred: redirect to error()
141      * @param exception SAXParseException
142      */
143     public final void warning(SAXParseException exception)
144     {
145         error(exception);
146     }
147 
148     /***
149      * log event to output
150      * @param e SAXParseException
151      * @param logLevel int
152      */
153     private void doLog(SAXParseException e, int logLevel)
154     {
155         Element error = new DefaultElement("error");
156         error.addAttribute("line", "" + e.getLineNumber());
157         error.addAttribute("col", "" + e.getColumnNumber());
158         error.addAttribute("level", "" + logLevel);
159 
160         error.addText(e.getMessage());
161         writeToLog(error);
162     }
163 
164     /***
165      * this method is not used to resolve entities, but to log dtd in files
166      * @param publicId public ID in dtd
167      * @param systemId system ID in dtd
168      * @return always null
169      */
170     public InputSource resolveEntity(String publicId, String systemId)
171     {
172         // log dtd info (not entities)
173         if (systemId.indexOf(".dtd") != -1)
174         {
175             // write a "dtd" element to output
176             Element dtd = new DefaultElement("dtd");
177             dtd.addAttribute("publicId", publicId);
178             dtd.addAttribute("systemId", systemId);
179             writeToLog(dtd);
180         }
181         return null;
182     }
183 
184     /***
185      * write an element to the xml log file
186      * @param element Element to write
187      */
188     private void writeToLog(Element element)
189     {
190         try
191         {
192             out.write(element);
193         }
194         catch (IOException e)
195         {
196             e.printStackTrace();
197         }
198     }
199 }